<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use App\Models\Traits\UserWatching;
use App\Notifications\UserFollowed;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable, UserWatching;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'avatar', 'introduction', 'weapp_openid', 'weixin_session_key', 'type'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];



    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }
    public function setting()
    {
        return $this->hasOne(UserSetting::class);
    }
    public function movies()
    {
        // 用户正在追的剧
        return $this->belongsToMany('App\Models\Movie', 'movie_watch_user')
            ->withPivot('status')
            ->withTimestamps();
    }

    public function collects()
    {
        // 用户的专辑
        return $this->hasMany(Collect::class);
    }

    public function scopeCollectCount($query)
    {
        return $query->withCount('collects');
    }
    // 用户和 电影关联关系
    public function watchStatusWithMovies($movieIds)
    {
        return MovieWatchUser::whereIn('movie_id', $movieIds)->where('user_id', $this->id)->get();
    }

    // 用户和用户的关系
    public function followers()
    {
        return $this->belongsToMany(User::class, 'followers', 'user_id', 'follower_id')->withTimestamps();
    }

    public function followings()
    {
        return $this->belongsToMany(User::Class, 'followers', 'follower_id', 'user_id')->withTimestamps();
    }
    public function scopefollowersCount($query)
    {
        return $query->withCount('followers');
    }
    public function scopefollowingsCount($query)
    {
        return $query->withCount('followings');
    }
    public function scopemoviesCount($query)
    {
        return $query->withCount('movies');
    }

    // 关注
    public function follow($user_ids)
    {
        if ( ! is_array($user_ids)) {
            $user_ids = compact('user_ids');
        }
        // 通知用户有一个新粉丝
        User::whereIn('id', $user_ids)->first()->notify(new UserFollowed($this));
        $this->followings()->sync($user_ids, false);
    }
    // 取消关注
    public function unfollow($user_ids)
    {
        if ( ! is_array($user_ids)) {
            $user_ids = compact('user_ids');
        }

        $this->followings()->detach($user_ids);
    }
    // 是否关注
    public function isFollowing($user_id)
    {
        return $this->followings->contains($user_id);
    }

    // 重写通知中的 notify 方法
    use Notifiable {
        notify as protected laravelNotify;
    }
    public function notify($instance)
    {
        // 只有数据库类型通知才需提醒，直接发送 Email 或者其他的都 Pass
        if (method_exists($instance, 'toDatabase')) {
            $this->increment('notification_count');
        }

        $this->laravelNotify($instance);
    }

    public function markAsRead()
    {
        $this->notification_count = 0;
        $this->save();
        $this->unreadNotifications->markAsRead();
    }

    // 用户的评论
    public function comments()
    {
        return $this->hasMany(Comment::class, 'user_id');
    }
}
